var Login = function () {

    var handleLogin = function () {

        var ACCESS_TYPE = {
            ACCESS_ADMIN: 1,
            ACCESS_SERVER: 2,
            ACCESS_REPORT: 3,

        }


        $('.login-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                },
                remember: {
                    required: false
                }
            },

            messages: {
                username: {
                    required: "Username is required."
                },
                password: {
                    required: "Password is required."
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit
                $('.alert-danger', $('.login-form')).text("请输入用户名和密码")
                $('.alert-danger', $('.login-form')).show();
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {

                //form.submit(); // form validation success, call ajax form submit
                var passwd = $("#idPassword").val();
                var username = $("#idUsername").val();
                var token = $("#idtoken").val();
                var hash = hex_md5(token + passwd);
                var c2slogin = {
                    "username": username,
                    "password": hash,
                    "authtoken":token,
                };
				
                $.post("/adminLogin", c2slogin)
                    .success(function(data){
                        switch (parseInt(data)) {
                            case ACCESS_TYPE.ACCESS_ADMIN:
                            case ACCESS_TYPE.ACCESS_SERVER:
                                window.location.replace("/adminHome");
                                break;
                            case ACCESS_TYPE.ACCESS_REPORT:
                                window.location.replace("/reportWeb");
                                break;
                            default:
                                window.location.replace("/404Web");
                        }

                    })
                    .error(function () {
                        $('.alert-danger', $('.login-form')).text("用户名密码错误").show();
                        //写错后清空密码，回去参考easyUI后补充
                    })
            }

        })


        $('.login-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.login-form').validate().form()) {
                    $('.login-form').submit(); //form validation success, call ajax form submit
                }
                return false;
            }
        });
    }


    return {
        //main function to initiate the module
        init: function () {

            handleLogin();


        }

    };

}();